/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Anthares
 */
@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Produit.findAll", query = "SELECT p FROM Produit p"),
    @NamedQuery(name = "Produit.findByProdId", query = "SELECT p FROM Produit p WHERE p.prodId = :prodId"),
    @NamedQuery(name = "Produit.findByPrixUnit", query = "SELECT p FROM Produit p WHERE p.prixUnit = :prixUnit"),
    @NamedQuery(name = "Produit.findByCategory", query = "SELECT p FROM Produit p WHERE p.categId.categId = :categId"),
    @NamedQuery(name = "Produit.findByImage", query = "SELECT p FROM Produit p WHERE p.image = :image")})
public class Produit implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "PROD_ID", nullable = false)
    private Integer prodId;

    @Basic(optional = false)
    @NotNull
    @Column(name = "PRIX_UNIT", nullable = false)
    private double prixUnit;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "IMAGE", nullable = false, length = 255)
    private String image;

    @JoinTable(name = "PROMO_PROD", joinColumns = {
        @JoinColumn(name = "PROD_ID", referencedColumnName = "PROD_ID", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "PROMO_ID", referencedColumnName = "PROMO_ID", nullable = false)})
    @ManyToMany
    private List<Promotions> promotionsList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "produit")
    private List<LigneCommande> ligneCommandeList;

    @Transient
    private TradProd traduction;

    @JoinColumn(name = "CATEG_ID", referencedColumnName = "CATEG_ID", nullable = false)
    @ManyToOne(optional = false)
    private CategProd categId;
    
    @Transient
    private Promotions bestPromotions;

    public Produit() {
    }

    public Produit(Integer prodId) {
        this.prodId = prodId;
    }

    public Produit(Integer prodId, double prixUnit, String image) {
        this.prodId = prodId;
        this.prixUnit = prixUnit;
        this.image = image;
    }

    public TradProd getTraduction() {
        return traduction;
    }

    public void setTraduction(TradProd traduction) {
        this.traduction = traduction;
    }

    public Integer getProdId() {
        return prodId;
    }

    public void setProdId(Integer prodId) {
        this.prodId = prodId;
    }

    public double getPrixUnit() {
        return prixUnit;
    }

    public void setPrixUnit(double prixUnit) {
        this.prixUnit = prixUnit;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @XmlTransient
    public List<Promotions> getPromotionsList() {
        return promotionsList;
    }

    public void setPromotionsList(List<Promotions> promotionsList) {
        this.promotionsList = promotionsList;
    }

    @XmlTransient
    public List<LigneCommande> getLigneCommandeList() {
        return ligneCommandeList;
    }

    public void setLigneCommandeList(List<LigneCommande> ligneCommandeList) {
        this.ligneCommandeList = ligneCommandeList;
    }

    public CategProd getCategId() {
        return categId;
    }

    public void setCategId(CategProd categId) {
        this.categId = categId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (prodId != null ? prodId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if(!(object instanceof Produit)){
            return false;
        }
        Produit other = (Produit) object;
        if((this.prodId == null && other.prodId != null) || (this.prodId != null && !this.prodId.equals(other.prodId))){
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Model.Produit[ prodId=" + prodId + " ]";
    }

    public Promotions getBestPromotions() {
        return bestPromotions;
    }

    public void setBestPromotions(Promotions bestPromotions) {
        this.bestPromotions = bestPromotions;
    }
    
    public double getReducedPrice(){
        if(bestPromotions==null && !categId.isOnPromotion()){
            return this.prixUnit;
        }
        if(categId.isOnPromotion()) return this.prixUnit*this.categId.getBestPromo().getPourcentage();
        return this.prixUnit*this.bestPromotions.getPourcentage();
    }
    public boolean isOnPromotion(){
        return this.categId.isOnPromotion() || this.bestPromotions!=null;
    }
}
